/**********************************************************
* Description:                                            *
*         This demo show you how to implement accept      *
*         a command y/n without enter key.                *
*                                                         *
**********************************************************/
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char ch;
    struct termios old_term;
    struct termios cur_term;
    tcgetattr(STDIN_FILENO, &old_term);
    memcpy(&cur_term, &old_term, sizeof(cur_term));
    cur_term.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
    cur_term.c_cc[VMIN] = 1;
    cur_term.c_cc[VTIME] = 0;
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &cur_term);

    printf("input control key:");
    while (1)
    {
        ch = getchar();
        if (ch == '\r')
        {
            printf("\nenter key");
            continue;
        }
        if (ch == '\33')
        {
            ch = getchar();
            if (ch == '[')
            {
                ch = getchar();
                switch (ch)
                {
                    case 'A':
                        printf("\nMOVEUP");
                        break;
                    case 'B':
                        printf("\nMOVEDOWN");
                        break;
                    case 'C':
                        printf("\nMOVERIGHT");
                        break;
                    case 'D':
                        printf("\nMOVELEFT");
                        break;
                }
            }
        }
        if (ch == 'q' || ch == 'Q')
        {
            printf("\n");
            break;
        }
        fflush(stdout);
    }
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &old_term);

    return 0;
}
